<template>
  <div>
    <h1>修改用户</h1>
    用户名：<input type="text" v-model.trim="name" /><br />
    年龄:<input type="number" v-model.trim.number="age" /><br />
    <button @click="submit">提交</button>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

import { getUserByIdApi, updateUserApi } from '@/api/users.js'

const route = useRoute()
const router = useRouter()

const id = route.params.id
const name = ref('')
const age = ref('')

// 回显数据
// 1.解析id参数(useRoute)
// 2.发送请求，根据id查询用户的信息

onMounted(async () => {
  const res = await getUserByIdApi(id)
  name.value = res.name
  age.value = res.age
})

const submit = async () => {
  await updateUserApi(id, { name: name.value, age: age.value })
  alert('修改成功')
  router.push('/')
}
</script>

<style scoped></style>
